package com.df.tools.handler;

import com.df.tools.msg.MsgDefinition;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
@RequiredArgsConstructor
public class MsgDefinitionHandlerContainer implements IHandler {


    private final Map<String, IHandler> handlerMap;


    @Override
    public boolean canHandle() {
        //一定要返回FALSE，不然死循环了
        return false;
    }

    @Override
    public void handle(List<MsgDefinition> msgDefinitions) {
        if(handlerMap==null|| handlerMap.size()<=0){
            log.error("未收集到任何IHandler,处理失败！");
            return;
        }
        Collection<IHandler> handlers = handlerMap.values();
        for (IHandler handler : handlers) {
            try {
                if(handler == this) {
                    continue;
                }
                if (!handler.canHandle()) {
                    continue;
                }
                handler.handle(msgDefinitions);
            } catch (Exception e) {
                log.error("处理器处理失败：" + handler.getClass().getName());
                e.printStackTrace();
            }

        }
    }
}
